<?php
include "connect/connect_dsccmis.php";

if (!$db = mysql_connect($host, $user, $pass)) {
	echo 'Could not connect to mysql';
	exit;
}
if (!mysql_select_db($dbname, $db)) {
	echo 'Could not select database';
	exit;
}
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
//XML Header
header("content-type:text/xml");
mysql_query("set names utf8");
$cond_date_star = "";
$cond_date_end = "";
if($chkuse_date){
	$cond_date_star = " and evndte >='".$date_start."'";
	$cond_date_end = " and evndte <='".$date_end."'";
}

$cond_prov = "";
if($chkuse_prov){
  	$cond_prov=$chkbox90." ".$chkbox94." ".$chkbox95." ".$chkbox96;
	$cond_prov=trim($cond_prov);
	$cond_prov=preg_replace("/\s+/","','",$cond_prov);
	$cond_prov="('".$cond_prov."')";
	$cond_prov=" and prov in ".$cond_prov;
	//if($chkbox00=="00"){
	//	$cond_prov = $cond_prov." and prov not in ('90','94','95','96')";
	//}
}
// เซ็ตเงื่อนไขสำหรับ ประเภทเหตุการณ์
$cond_typ=$chkbox_type1." ".$chkbox_type2." ".$chkbox_type3." ".$chkbox_type4." ".$chkbox_type9;
$cond_typ=trim($cond_typ);
$cond_typ=preg_replace("/\s+/","','",$cond_typ);
$cond_typ="('".$cond_typ."')";
$cond_typ=" and mis_evntypid in ".$cond_typ;

$query = "SELECT evnid,evnnme,evndte,evntme,evndes,mis_evntypid,tarx,tary,lat,lng FROM gis_event where 1 ".$cond_date_star.$cond_date_end.$cond_prov.$cond_typ;
//echo $query;
$query = mysql_query($query);
echo "<locations>";
while ($row=mysql_fetch_assoc($query)){
	if((strlen($row['tary'])==6) && (strlen($row['tarx'])==6)){
	  $latlng_cal = LatLonPointUTMtoLL($row['tary'],$row['tarx'],47);
	  $lat_use = $latlng_cal['lat'];
	  $lng_use = $latlng_cal['lng'];
	}else{
	  $lat_use = $row['lat'];
	  $lng_use = $row['lng'];
	}
	$evnnme_use = str_replace('"','&quot;',$row['evnnme']);
	echo "<location id=\"".$row['evnid']."\" evnnme=\"".$evnnme_use."\" evntme=\"".$row['evntme']."\" evndte=\"".$row['evndte']."\" min_new_evntypid=\"".$row['mis_evntypid']."\" lat=\"".$lat_use."\" lng=\"".$lng_use."\"/>";
}
echo "</locations>";




//------------------------------------------------------------------------
	function LatLonPointUTMtoLL($f, $f1, $j=47) { //y,x in UTM
// $f = Northing    => Y
// $f1 = Easting    => X
// $j: UTM ZONE (Danmark = 32)

$d = 0.99960000000000004; // scale along long0
$d1 = 6378137; // Polar Radius
$d2 = 0.0066943799999999998;

$d4 = (1 - sqrt(1 - $d2)) / (1 + sqrt(1 - $d2));
$d15 = $f1 - 500000;
$d16 = $f;
$d11 = (($j - 1) * 6 - 180) + 3;
$d3 = $d2 / (1 - $d2);
$d10 = $d16 / $d;
$d12 = $d10 / ($d1 * (1 - $d2 / 4 - (3 * $d2 * $d2) / 64 - (5 * pow($d2,3) ) / 256));
$d14 = $d12 + ((3 * $d4) / 2 - (27 * pow($d4,3) ) / 32) * sin(2 * $d12) + ((21 * $d4 * $d4) / 16 - (55 * pow($d4,4) ) / 32) * sin(4 * $d12) + ((151 * pow($d4,3) ) / 96) * sin(6 * $d12);
$d13 = rad2deg($d14);
$d5 = $d1 / sqrt(1 - $d2 * sin($d14) * sin($d14));
$d6 = tan($d14) * tan($d14);
$d7 = $d3 * cos($d14) * cos($d14);
$d8 = ($d1 * (1 - $d2)) / pow(1 - $d2 * sin($d14) * sin($d14), 1.5);
$d9 = $d15 / ($d5 * $d);
$d17 = $d14 - (($d5 * tan($d14)) / $d8) * ((($d9 * $d9) / 2 - (((5 + 3 * $d6 + 10 * $d7) - 4 * $d7 * $d7 - 9 * $d3) * pow($d9,4) ) / 24) + (((61 + 90 * $d6 + 298 * $d7 + 45 * $d6 * $d6) - 252 * $d3 - 3 * $d7 * $d7) * pow($d9,6) ) / 720);
$d17 = rad2deg($d17); // Breddegrad (N)
$d18 = (($d9 - ((1 + 2 * $d6 + $d7) * pow($d9,3) ) / 6) + (((((5 - 2 * $d7) + 28 * $d6) - 3 * $d7 * $d7) + 8 * $d3 + 24 * $d6 * $d6) * pow($d9,5) ) / 120) / cos($d14);
$d18 = $d11 + rad2deg($d18); // LÃ¦ngdegrad (Ã˜)
return array('lat'=>$d17,'lng'=>$d18);
}
?>
